package mz.behaveas.model.expectation {
	import mz.behaveas.model.ducktype.Duck;

	/**
	 * @author maxim.zaks@gmail.com
	 */
	public class ThrowExpectation implements Expectation {

		
		private var _description : String;
		private var behaviour : Function;
		private var error : *;
		private var actError : Error = null;

		public function ThrowExpectation(description : String, behaviour : Function, error : *) {
			this.error = error;
			this.behaviour = behaviour;
			_description = description;
		}

		public function get description() : String {
			return _description;
		}

		public function get isCorrect() : Boolean {
			var result : Boolean = false;
			try {
				behaviour();
			}catch(e : Error) {
				actError = e;
				if(isExpectedError(e)) {
					result = true;
				} else {
					result = false;
				}
			}
			return result;
		}
		
		private function isExpectedError(e : Error) : Boolean {
			if(error is Class){
				return e is error;
			}
			return Duck(error).equals(e);
		}

		public function get report() : String {
			if(isCorrect) {
				return FLAG[0] + " The behavioure was interrupted as expected. " + actError;
			} else {
				if(actError != null) {
					return FLAG[1] + " The bahavioure was not interrupted by " + error + " but by " + actError;
				}
			}
			return FLAG[1] + " The bahavioure was not interrupoted. We expected interruption by " + error;
		}
		
		public function getError() : Error {
			return actError;
		}
	}
}
